package com.shiloop.sensorsampler;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment implements SensorEventListener {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private WaveView mXView;
    private WaveView mYView;
    private WaveView mZView;
    private View mWaveViewWrapper;
    private ColorView mColorView;
    private ArcView mArcView;

    private int mColorViewVisible = 0;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    public void switchView() {
        mColorViewVisible = (mColorViewVisible + 1) % 3;
        switch (mColorViewVisible) {
            case 0:
                mXView.reset();
                mYView.reset();
                mZView.reset();
                mColorView.setVisibility(View.GONE);
                mArcView.setVisibility(View.GONE);
                mWaveViewWrapper.setVisibility(View.VISIBLE);
                break;
            case 1:
                mColorView.reset();
                mColorView.setVisibility(View.VISIBLE);
                mArcView.setVisibility(View.GONE);
                mWaveViewWrapper.setVisibility(View.GONE);
                break;
            case 2:
                mArcView.reset();
                mColorView.setVisibility(View.GONE);
                mArcView.setVisibility(View.VISIBLE);
                mWaveViewWrapper.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_sensor_sampler, container, false);
        mXView = (WaveView) mRootView.findViewById(R.id.x_view);
        mYView = (WaveView) mRootView.findViewById(R.id.y_view);
        mZView = (WaveView) mRootView.findViewById(R.id.z_view);
        mColorView = (ColorView) mRootView.findViewById(R.id.color_view);
        mWaveViewWrapper = mRootView.findViewById(R.id.wave_view);
        mArcView = (ArcView) mRootView.findViewById(R.id.arc_view);
        mColorView.setVisibility(View.GONE);
        mArcView.setVisibility(View.GONE);
        return mRootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((SensorSamplerActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (mColorViewVisible) {
            case 0:
                if (null != mXView) {
                    mXView.add(event.values[0]);
                }
                if (null != mYView) {
                    mYView.add(event.values[1]);
                }
                if (null != mZView) {
                    mZView.add(event.values[2]);
                }
                break;
            case 1:
                mColorView.add(event.values);
                break;
            case 2:
                mArcView.add(event.values);
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
